package com.example.My.service.context;

import com.example.My.common.ResponseResult;
import com.example.My.pojo.LoginUser;
import com.example.My.pojo.User;
import com.example.My.service.Impl.LoginServiceImpl;
import com.example.My.service.LoginService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * Author: 谢宇
 * CreatTime 2023/11/11 17:13
 * Version 1.0.0
 */
public class LoginContext {


    LoginService loginService;

    private final static HashMap<String,String> map ;

    //传入一个map空对象，通过type去掉用里面的方法

    /**
     * todo 初始化用于反射的方法
     */
    static {
        map=new HashMap<>();
        map.put("1","login");
        map.put("2","loginByEmail");
        map.put("3","loginByPhone");
    }


    public LoginContext(LoginService loginService) {
        this.loginService=loginService;
    }

    /**
     * todo 反射去执行方法,通过传入type值不同去执行不同的方法
     * @param type
     */
    public ResponseResult doThings(String type, User user) throws NoSuchMethodException {
        String methodName=map.get(type);
        Class<LoginServiceImpl> loginServiceClass = LoginServiceImpl.class;
        Method declaredMethod = loginServiceClass.getDeclaredMethod(methodName, User.class);
        try {
             ResponseResult invoke = (ResponseResult) declaredMethod.invoke(this.loginService, user);
             return invoke;
        } catch (IllegalAccessException|InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

//    public static void main(String[] args) {
//        System.out.println(map);
//    }
}
